

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>foampy.dictionaries &mdash; foamPy 0.0.5 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../genindex.html"/>
        <link rel="search" title="Search" href="../../search.html"/>
    <link rel="top" title="foamPy 0.0.5 documentation" href="../../index.html"/>
        <link rel="up" title="Module code" href="../index.html"/> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> foamPy
          

          
          </a>

          
            
            
              <div class="version">
                0.0.5
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">foamPy</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>foampy.dictionaries</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for foampy.dictionaries</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Functions for reading and writing dictionaries.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span><span class="p">,</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">os</span>


<span class="n">system_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;controlDict&quot;</span><span class="p">,</span> <span class="s2">&quot;snappyHexMeshDict&quot;</span><span class="p">,</span> <span class="s2">&quot;fvSchemes&quot;</span><span class="p">,</span> <span class="s2">&quot;fvSolution&quot;</span><span class="p">,</span>
                <span class="s2">&quot;fvOptions&quot;</span><span class="p">,</span> <span class="s2">&quot;decomposeParDict&quot;</span><span class="p">,</span> <span class="s2">&quot;topoSetDict&quot;</span><span class="p">,</span> <span class="s2">&quot;blockMeshDict&quot;</span><span class="p">]</span>
<span class="n">constant_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;dynamicMeshDict&quot;</span><span class="p">,</span> <span class="s2">&quot;RASProperties&quot;</span><span class="p">,</span> <span class="s2">&quot;transportProperties&quot;</span><span class="p">,</span>
                  <span class="s2">&quot;turbulenceProperties&quot;</span><span class="p">]</span>

<span class="n">upper_rule</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;// * * * * * * * * * * * * * * * * * * * * * * * * * &quot;</span>
             <span class="s2">&quot;* * * * * * * * * * * * //&quot;</span><span class="p">)</span>
<span class="n">lower_rule</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;// **********************************************&quot;</span>
             <span class="s2">&quot;*************************** //&quot;</span><span class="p">)</span>


<div class="viewcode-block" id="build_header"><a class="viewcode-back" href="../../foampy.html#foampy.dictionaries.build_header">[docs]</a><span class="k">def</span> <span class="nf">build_header</span><span class="p">(</span><span class="n">dictobject</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s2">&quot;2.3.x&quot;</span><span class="p">,</span> <span class="n">fileclass</span><span class="o">=</span><span class="s2">&quot;dictionary&quot;</span><span class="p">,</span>
                 <span class="n">incl_foamfile</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Creates the header for an OpenFOAM dictionary. Inputs are the</span>
<span class="sd">    object and version.&quot;&quot;&quot;</span>
    <span class="n">txt</span> <span class="o">=</span> \
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;/*--------------------------------*- C++ -*----------------------------------*\</span>
<span class="sd">| =========                 |                                                 |</span>
<span class="sd">| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |</span>
<span class="sd">|  \\    /   O peration     | Version:  {}                                 |</span>
<span class="sd">|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |</span>
<span class="sd">|    \\/     M anipulation  |                                                 |</span>
<span class="sd">\*---------------------------------------------------------------------------*/&quot;&quot;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">version</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">incl_foamfile</span><span class="p">:</span>
        <span class="n">txt</span> <span class="o">+=</span> \
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">FoamFile</span>
<span class="sd">{{</span>
<span class="sd">    version     2.0;</span>
<span class="sd">    format      ascii;</span>
<span class="sd">    class       {};</span>
<span class="sd">    object      {};</span>
<span class="sd">}}&quot;&quot;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">fileclass</span><span class="p">,</span> <span class="n">dictobject</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">txt</span></div>


<div class="viewcode-block" id="replace_value"><a class="viewcode-back" href="../../foampy.html#foampy.dictionaries.replace_value">[docs]</a><span class="k">def</span> <span class="nf">replace_value</span><span class="p">(</span><span class="n">dictpath</span><span class="p">,</span> <span class="n">keyword</span><span class="p">,</span> <span class="n">newvalue</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Replace a value in a dictionary.&quot;&quot;&quot;</span>
    <span class="n">newvalue</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">newvalue</span><span class="p">)</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">dictpath</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">in_block</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
        <span class="n">single_line</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)):</span>
            <span class="n">sl</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sl</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">sl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">keyword</span><span class="p">:</span>
                <span class="n">nstart</span> <span class="o">=</span> <span class="n">n</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]:</span>
                    <span class="n">in_block</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="n">single_line</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="ow">and</span> <span class="n">in_block</span><span class="p">:</span>
                <span class="n">in_block</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="n">nend</span> <span class="o">=</span> <span class="n">n</span>
    <span class="k">if</span> <span class="n">single_line</span><span class="p">:</span>
        <span class="n">oldvalue</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">nstart</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">newvalue</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">nstart</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">oldvalue</span><span class="p">,</span> <span class="n">newvalue</span><span class="p">)</span>
        <span class="n">new_text</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[:</span><span class="n">nstart</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">newvalue</span><span class="p">]</span> <span class="o">+</span> <span class="n">lines</span><span class="p">[</span><span class="n">nstart</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">new_text</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[:</span><span class="n">nstart</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">newvalue</span><span class="p">]</span> <span class="o">+</span> <span class="n">lines</span><span class="p">[</span><span class="n">nend</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">dictpath</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">new_text</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span></div>


<div class="viewcode-block" id="read_text"><a class="viewcode-back" href="../../foampy.html#foampy.dictionaries.read_text">[docs]</a><span class="k">def</span> <span class="nf">read_text</span><span class="p">(</span><span class="n">dictpath</span><span class="p">,</span> <span class="n">keyword</span><span class="p">):</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">dictpath</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">in_block</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
        <span class="n">single_line</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)):</span>
            <span class="n">sl</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sl</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">sl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">keyword</span><span class="p">:</span>
                <span class="n">nstart</span> <span class="o">=</span> <span class="n">n</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]:</span>
                    <span class="n">in_block</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="n">single_line</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="ow">and</span> <span class="n">in_block</span><span class="p">:</span>
                <span class="n">in_block</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="n">nend</span> <span class="o">=</span> <span class="n">n</span>
    <span class="k">return</span> <span class="n">lines</span><span class="p">[</span><span class="n">nstart</span><span class="p">:</span><span class="n">nend</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span></div>


<div class="viewcode-block" id="read_single_line_value"><a class="viewcode-back" href="../../foampy.html#foampy.dictionaries.read_single_line_value">[docs]</a><span class="k">def</span> <span class="nf">read_single_line_value</span><span class="p">(</span><span class="n">dictname</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dictpath</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">keyword</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span>
                           <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">,</span> <span class="n">casedir</span><span class="o">=</span><span class="s2">&quot;./&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Read value from a dictionary that appears on a single line.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">dictpath</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">dictname</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">dictname</span> <span class="ow">in</span> <span class="n">system_dicts</span><span class="p">:</span>
            <span class="n">dictpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">casedir</span><span class="p">,</span> <span class="s2">&quot;system&quot;</span><span class="p">,</span> <span class="n">dictname</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">dictname</span> <span class="ow">in</span> <span class="n">constant_dicts</span><span class="p">:</span>
            <span class="n">dictpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">casedir</span><span class="p">,</span> <span class="s2">&quot;constant&quot;</span><span class="p">,</span> <span class="n">dictname</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">dictpath</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">dictname</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Neither dictionary name nor dictionary path supplied&quot;</span><span class="p">)</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">dictpath</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="s2">&quot;;&quot;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
                <span class="n">ls</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;;&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">ls</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">keyword</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">dtype</span><span class="p">(</span><span class="n">ls</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></div>
</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2017, Pete Bachant.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'0.0.5',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>